using Godot;
using System;

public partial class Em : CharacterBody2D
{
	[Export] int speed = 50;
	[Export] public Play play_node;
    public TileMapLayer ditu;
	public NavigationAgent2D navigation2d;
    public bool isMove = false;
    public ulong kaishituolishiji;
    public ulong tuolishiji = 500;
    public bool shiji=false;
    public Vector2 yuandian;
    public Godot.Collections.Array<Vector2I> lujing = new Godot.Collections.Array<Vector2I>();

    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
	{
        ditu = GetParent().GetNode<TileMapLayer>("TileMapLayer");
        navigation2d = GetNode<NavigationAgent2D>("navigation/NavigationAgent2D");
        yuandian = Position;
    }

	// Called every frame. 'delta' is the elapsed time since the previous frame.

    public override void _PhysicsProcess(double delta)
    {

        var juli = Math.Abs((play_node.Position - Position).Length());
        //if (!navigation2d.IsNavigationFinished())
        //{
        //    if (isMove)
        //    {

        //        var direction = ToLocal(navigation2d.GetNextPathPosition()).Normalized();
        //        var Vel = direction * speed;
        //        navigation2d.SetVelocityForced(Vel);


        //    }
        //    else
        //    {
        //        if (yuandian == Position)
        //        {
        //            Velocity = Vector2.Zero;
        //        }
        //        else
        //        {
        //            var direction = ToLocal(navigation2d.GetNextPathPosition()).Normalized();
        //            var Vel = direction * speed;
        //            navigation2d.SetVelocityForced(Vel);
        //            //Velocity = direction * speed;
        //        }
        //    }
        //    MoveAndSlide();
        //}
        if (!isMove&&juli < 200 && juli > 10)
        {
            isMove = true;

        }
        if (isMove && juli > 320 && !shiji)
        {
            // 为停止追击开始倒计时
            kaishituolishiji = Time.GetTicksMsec();

            shiji = true;

        }
        if (shiji && kaishituolishiji + tuolishiji < Time.GetTicksMsec())
        {
            isMove = false;
            shiji = false;
        }

        if (lujing != null && lujing.Count > 0)
        {
            var target_position = ditu.MapToLocal(lujing[0]);
            if (GlobalPosition == target_position)
            {
                lujing.RemoveAt(0);
            }
            //var dir = ditu.MapToLocal(lujing[0]) - GlobalPosition;
            GlobalPosition = GlobalPosition.MoveToward(target_position, 100*(float)delta);
            //Velocity = dir * speed;
            //GD.Print(lujing);
        }


        //MoveAndSlide();
    }

    public void _on_timer_timeout()
    {
        if (isMove)
        {
            //lujing.Clear();
            //navigation2d.TargetPosition = play_node.Position;
            lujing = ditu.AStarGrid.GetIdPath(ditu.LocalToMap(Position), ditu.LocalToMap(play_node.Position));
            GD.Print("追击");

        }
        else
        {
            //lujing.Clear();
            //navigation2d.TargetPosition = yuandian;
            lujing = ditu.AStarGrid.GetIdPath(ditu.LocalToMap(Position), ditu.LocalToMap(yuandian));
            GD.Print("回归");
        }
    }

    public void _on_navigation_agent_2d_velocity_computed(Vector2 safe_velocity)
    {

        Velocity = safe_velocity;
        //MoveAndSlide();
      
    }

}
